<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/css/public.css" media="all">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        #dictTable + .layui-table-view .layui-table-tool-temp {
            padding-right: 0;
        }

        #dictTable + .layui-table-view .layui-table-body tbody > tr td {
            cursor: pointer;
        }

        #dictTable + .layui-table-view .layui-table-body tbody > tr.layui-table-click {
            background-color: #fff3e0;
        }

        #dictTable + .layui-table-view .layui-table-body tbody > tr.layui-table-click td:last-child > div:before {
            position: absolute;
            right: 6px;
            content: "\e602";
            font-size: 12px;
            font-style: normal;
            font-family: layui-icon !important;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }

    </style>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid" style="padding-bottom: 0;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 10px;">
                    <!-- 表格工具栏1 -->
                    <form class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline" style="max-width: 160px;">
                                <input name="name" class="layui-input" placeholder="输入角色名"/>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn icon-btn" lay-filter="dictTbSearch" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                            </div>
                        </div>
                    </form>
                    <!-- 数据表格1 -->
                    <table id="dictTable" lay-filter="dictTable"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 10px;">
                    <!-- 表格工具栏2 -->
                    <form class="layui-form toolbar">
                        <div class="layui-inline">
                            <label class="layui-form-label">性&emsp;别:</label>
                            <div class="layui-input-inline">
                                <select name="sex">
                                    <option value="">选择性别</option>
                                    <option value="1">男</option>
                                    <option value="2">女</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状&emsp;态:</label>
                            <div class="layui-input-inline">
                                <select name="state">
                                    <option value="">选择状态</option>
                                    <option value="0">正常</option>
                                    <option value="1">禁用</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">关键词:</label>
                            <div class="layui-input-inline">
                                <input name="search" class="layui-input" placeholder="姓名/手机/用户名"/>
                            </div>
                        </div>
                        <div class="layui-inline">&emsp;
                            <button class="layui-btn icon-btn" lay-filter="dictDataTbSearch" lay-submit>
                                <i class="layui-icon">&#xe615;</i>搜索
                            </button>
                        </div>
                    </form>
                    <!-- 数据表格2 -->
                    <table id="dictDataTable" lay-filter="dictDataTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 表单弹窗1 -->
<script type="text/html" id="dictEditDialog">
    <div class="layuimini-main">
        <form id="dictEditForm" lay-filter="dictEditForm" class="layui-form layuimini-form">
            <input name="id" type="hidden"/>
            <div class="layui-form-item">
                <label class="layui-form-label required">角色名</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入角色名" class="layui-input"
                           lay-verType="tips" lay-verify="required" required/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input type="text" name="sort" lay-verify="" value="0" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="dictEditSubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="dictDataTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="join">
    <input type="text" class="layui-input">
</script>

<!-- js部分 -->
<script src="/static/admin/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/admin/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['layer', 'form', 'table', 'tableSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var tableSelect = layui.tableSelect;
        var selObj;  // 左表选中数据

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#dictTable',
            url: '/api/system/role',
            height: 'full-100',
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="edit" class="layui-btn layui-btn-sm layui-btn-warm icon-btn"><i class="layui-icon">&#xe642;</i>修改</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '${if auth "PUT" "/api/system/role/permission"}<button lay-event="permission" class="layui-btn layui-btn-sm layui-btn-normal icon-btn"><i class="layui-icon layui-icon-top"></i>权限分配</button>${end}',
                '</p>'].join(''),
            defaultToolbar: [],
            cols: [[
                {field: 'id', title: 'ID', width: 80},
                {field: 'name', title: '角色名称'},
                {field: 'sort', title: '排序', hide: true},
                {field: 'remark', title: '备注', hide: true},
            ]],
            done: function (res, curr, count) {
                $('#dictTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
            }
        });

        /* 表格搜索 */
        form.on('submit(dictTbSearch)', function (data) {
            insTb.reload({where: data.field});
            return false;
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dictTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'edit') { // 修改
                showEditModel(selObj.data, selObj);
            } else if (obj.event === 'del') { // 删除
                doDel(selObj);
            } else if (obj.event === 'permission') { //权限分配
                layer.open({
                    type: 2,
                    title: '权限分配',
                    area: ['400px', '600px'],
                    content: "/admin/system/permission?role_id=" + selObj.data.id,
                    btn: ['保存', '取消'],
                    yes: function (index, layero) {
                        // 获取子页面的iframe
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        var ids = iframeWin.getIds();
                        $.ajax({
                            url: "/api/system/role/permission",
                            type: "put",
                            data: {ids: ids, role_id: selObj.data.id},
                            success: function (res) {
                                if (res.code === 0) {
                                    layer.msg(res.msg)
                                    layer.close(index)
                                } else {
                                    layer.msg(res.msg)
                                }
                            }
                        })
                    }
                })
            }
        });

        /* 监听行单击事件 */
        table.on('row(dictTable)', function (obj) {
            selObj = obj;
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            insTb2.reload({where: {role_id: obj.data.id}, page: {curr: 1}, url: '/api/system/admin'});
        });

        /* 显示表单弹窗 */
        function showEditModel(mData, obj) {
            layer.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '角色',
                area: ['400px', '350px'],
                content: $('#dictEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('dictEditForm', mData);
                    // 表单提交事件
                    form.on('submit(dictEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.ajax({
                            url: "/api/system/role",
                            type: mData ? 'PUT' : 'POST',
                            data: data.field,
                            success: function (res) {
                                layer.close(loadIndex);
                                if (0 === res.code) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    if (obj) {
                                        obj.update(data.field);
                                    } else {
                                        insTb.reload();
                                    }
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }

        /* 删除角色 */
        function doDel(obj) {
            layer.confirm('确定要删除此角色吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.ajax({
                    url: "/api/system/role",
                    type: "delete",
                    data: {id: obj.data.id},
                    success: function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            obj.del();
                            $('#dictTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        /* 渲染表格2 */
        var insTb2 = table.render({
            elem: '#dictDataTable',
            data: [],
            height: 'full-100',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>加入</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>移除</button>&nbsp;',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 80, fixed: 'left', unresize: true}
                , {field: 'user', title: '用户名', width: 120}
                , {
                    field: 'mobile', title: '手机号码', width: 150, templet: function (res) {
                        return '<em>' + res.mobile + '</em>'
                    }
                }
                , {
                    field: 'sex', title: '性别', width: 80, templet: function (res) {
                        if (res.sex === 1) {
                            return '<span style="color: dodgerblue">男</span>'
                        } else if (res.sex === 2) {
                            return '<span style="color: hotpink">女</span>'
                        } else {
                            return ""
                        }
                    }
                }
                , {field: 'name', title: '姓名', width: 100}
                , {
                    field: 'state', title: '状态', width: 100, templet: function (res) {
                        if (res.state === 0) {
                            return '<span style="color: green">正常</span>'
                        } else if (res.state === 1) {
                            return '<span style="color: red">禁用</span>'
                        } else {
                            return ""
                        }
                    }
                }
                , {field: 'remark', title: '备注'}
                , {field: 'create_time', title: '创建时间', width: 180}
            ]]
        });

        /* 表格2搜索 */
        form.on('submit(dictDataTbSearch)', function (data) {
            insTb2.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格2头工具栏点击事件 */
        table.on('toolbar(dictDataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                $("#join").click()
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dictDataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要移除的用户', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel2({ids: ids, role_id: selObj.data.id});
            }
        });

        //加入:用户列表
        tableSelect.render({
            elem: '#join',
            searchKey: 'search',
            checkedKey: 'id',
            width: '200',
            searchPlaceholder: '账号/姓名/手机号/',
            table: {
                url: '/api/system/admin',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID', width: 100},
                    {field: 'name', title: '姓名'},
                    {field: 'mobile', title: '手机号'},
                ]]
            },
            done: function (elem, data) {
                var ids = []
                layui.each(data.data, function (index, item) {
                    ids.push(item.id)
                })
                $.ajax({
                    url: "/api/system/role/user",
                    type: "post",
                    data: {role_id: selObj.data.id, ids: ids},
                    success: function (res) {
                        layer.msg(res.msg, function () {
                            if (res.code === 0) {
                                insTb2.reload({page: {curr: 1}});
                                return true
                            }
                            return false
                        })
                    }
                })
            }
        })

        /* 移除 */
        function doDel2(obj) {
            layer.confirm('确定要移除选中用户吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.ajax({
                    url: "/api/system/role/user",
                    type: "DELETE",
                    data: obj,
                    success: function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb2.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }
                })
            });
        }
    });
</script>
</body>
</html>